//
//  ViewController.m
//  CameraDemo
//
//  Created by apple on 2017/3/6.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ViewController.h"
#import "YYTCaptureView.h"
#import "YYTBottomView.h"
#import "Masonry.h"
#import "SensorManager.h"
@interface ViewController ()
//@property (nonatomic, strong)UIButton *captureBtn;//拍照按钮
@property (nonatomic, strong)UIButton *openCaptureBtn;//打开摄像头按钮
@property (nonatomic, strong)YYTBottomView *bottomView;
@property (nonatomic, strong) SensorManager *manager;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.openCaptureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.openCaptureBtn.frame = CGRectMake(50, 100, 80, 80);
    self.openCaptureBtn.backgroundColor = [UIColor redColor];
    [self.openCaptureBtn setTitle:@"开始" forState:UIControlStateNormal];
    [self.openCaptureBtn setTitle:@"停止" forState:UIControlStateSelected];
    [self.openCaptureBtn addTarget:self action:@selector(openCaptureAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.openCaptureBtn];


///
    YYTCaptureView *captureView = [[YYTCaptureView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:captureView];
    //插入图层在拍照按钮的下方
    [self.view.layer insertSublayer:captureView.captureLayer below:self.openCaptureBtn.layer];
    [captureView.session startRunning];
    
    self.bottomView = [[YYTBottomView alloc]init];
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(175);
        make.bottom.mas_equalTo(-10);
    }];

    [self startSensor];
}

//- (void)openCaptureAction:(UIButton *)sender {
//    sender.selected = !sender.selected;
//    if (sender.selected) {
//        [self.session startRunning];//开始捕捉
//    } else {
//        [self.session stopRunning];//停止捕捉
//    }
//}
/**
 *  启动传感器
 */
- (void)startSensor
{
    __weak typeof(self)mySelf = self;
    _manager = [SensorManager shared];
    
    _manager.updateDeviceMotionBlock = ^(CMDeviceMotion *data){
        double gravityX = data.gravity.x;
        double gravityY = data.gravity.y;
        double gravityZ = data.gravity.z;
        double zTheta = atan2(gravityZ, sqrtf(gravityX * gravityX + gravityY * gravityY)) / M_PI * 180.0;
        double xyTheta = atan2(gravityX, gravityY) / M_PI * 180.0;
        mySelf.bottomView.zThetaLab.text = [NSString stringWithFormat:@"水平面夹角: %.4f",zTheta];
        mySelf.bottomView.xyThetaLab.text = [NSString stringWithFormat:@"旋转角度: %.4f",xyTheta];
        mySelf.bottomView.lab1.text = [NSString stringWithFormat:@"x : %.4f",gravityX];
        mySelf.bottomView.lab2.text = [NSString stringWithFormat:@"y : %.4f",gravityY];
        mySelf.bottomView.lab3.text = [NSString stringWithFormat:@"z : %.4f",gravityZ];
    };
    _manager.didiUpdateHeadingBlock = ^ (CLLocationDirection theHeading) {
        mySelf.bottomView.angleLab.text = [NSString stringWithFormat:@"从正北顺时针旋转角度 : %.4f",theHeading];
    };
    [_manager startGyroscope];
    [_manager startSensor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
